package com.gaohe.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gaohe.mapper.*;
import com.gaohe.pojo.*;
import com.gaohe.service.*;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
    @Autowired
    ClassesMapper classesMapper;
    @Autowired
    CardMapper cardMapper;
    @Autowired
    UserCardMapper userCardMapper;
    @Autowired
    UserClassMapper userClassMapper;

    @Override
    public List<Classes> findbyIdtoClass(Long id) {
        LambdaQueryWrapper<Classes> qw = new LambdaQueryWrapper<>();
        qw.eq(Classes::getTeacherId,id);
        List<Classes> list = classesMapper.selectList(qw);
        return list;
    }

    @Override
    public List<UserCard> findByIdtoCards(Long id) {
        LambdaQueryWrapper<UserCard> qw = new LambdaQueryWrapper<>();
        qw.eq(UserCard::getUserId,id);
        List<UserCard> userCards = userCardMapper.selectList(qw);
        return userCards;
    }

    @Override
    public Boolean saveByCardId(Long cardId, User user) {
        UserCard userCard = new UserCard();
        userCard.setUserId(user.getId());
        userCard.setCardId(cardId);
        LambdaQueryWrapper<Card> qw = new LambdaQueryWrapper<>();
        qw.eq(Card::getId,cardId);
        Card card = cardMapper.selectOne(qw);
        userCard.setCardName(card.getName());
        BeanUtils.copyProperties(card,userCard,"id","createTime","updateTime","createUser","updateUser","cardName");
        int insert = userCardMapper.insert(userCard);
        if(insert<0){
            return false;
        }
        else
        {
            return true;
        }

    }

    @Override
    public List<Classes> findByIdtoClass(Long id) {
        LambdaQueryWrapper<UserClass> qw = new LambdaQueryWrapper<>();
        qw.eq(UserClass::getUserId,id);
        List<UserClass> userClasses = userClassMapper.selectList(qw);
        ArrayList<Long> ids = new ArrayList<>();
        for (UserClass userClass : userClasses) {
            ids.add(userClass.getClassId());
        }
        LambdaQueryWrapper<Classes> qw1 = new LambdaQueryWrapper<>();
        qw1.in(Classes::getId,ids);
        List<Classes> classes = classesMapper.selectList(qw1);
        return classes;
    }


    @Override
    public Boolean getSelectedCard(Long userId, Long cardId) {
        LambdaQueryWrapper<UserCard> qw = new LambdaQueryWrapper<>();
        qw.eq(UserCard::getUserId,userId);
        qw.eq(UserCard::getCardId,cardId);
        qw.lt(UserCard::getEndDate, LocalDateTime.now());
        UserCard userCard = userCardMapper.selectOne(qw);
        if(userCard==null){
            return false;
        }
        else {
            return true;
        }
    }
}
